﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using Forum.Entities;

public partial class _404 : System.Web.UI.Page
{
    public string _Url = Public.Common.MRequest.GetQueryString("url");
    public int _ID = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        string txt = string.Empty;
        _ID = 0;

        //404;http://top.51renti.net:80/top.51renti.net/topic/9/201010/show-id457.html
        //404;http://top.51renti.net:80/top.51renti.net/topic/9/201010/show-page5-count12-id452.html

        if (!IsPostBack)
        {
            Regex r = new Regex(@"(-id)(\d+)(.html)", RegexOptions.IgnoreCase);
            Match m;
            for (m = r.Match(_Url); m.Success; m = m.NextMatch())
            {
                //txt ="<br>0=="+ m.Groups[0].ToString();
                //txt += "<br>1==" + m.Groups[1].ToString();
                //txt += "<br>2==" + m.Groups[2].ToString();
                //txt += "<br>3==" + m.Groups[3].ToString();

                _ID = int.Parse(m.Groups[2].ToString());
            }
            //this.Context.Response.Write(txt);
        }

        if (_Url.ToLower().IndexOf(".html") != -1)
        {
            Public.Common.Files.WriteLine(AppDomain.CurrentDomain.BaseDirectory + "log\\" + DateTime.Now.ToString("yyyy-MM-") + "url.html", DateTime.Now.ToString() + " ==== " + _Url + "<br>");
        }

        string indexstr = "top.51renti.net";

        if (_Url.ToLower().IndexOf("sex.vs2010.com") != -1)
        {
            this.Context.Response.Redirect("http://sex.vs2010.com/404.aspx?url=" + _Url);
        }
        else if (_Url.ToLower().IndexOf(indexstr) != -1)
        {
            if (_ID != 0)
            {
                forum_bodyinfo model = Forum.Web.CacheRepository.GetServices().forum_body.GetByBodyId(_ID);
                if (model != null)
                {
                    AddHtml(model);
                }
                this.Context.Response.Redirect("http://" + Regex.Split(_Url, ":80/")[1]);
            }
        }
        else
        {
            this.Repeater1.DataSource = WebBase.CacheRepository.forum_body.GetPaged("", " body_id desc ", 0, 40);
            this.Repeater1.DataBind();
        }
    }

    public void AddHtml(forum_bodyinfo model)
    {
        string body = string.Empty;
        Forum.Entities.forum_bodybodyinfo model2 = Forum.Web.Repository.GetServices().forum_bodybody.GetByBodyBodyId(model.Body_ID);
        if (model != null)
        {
            body = model2.BodyBody_Body;
        }
        Forum.StaticHtml.AddHtmlFile.Instance().InID(model.Body_ID, model.Body_MuLuID.Value, model.Body_AddTime.Value, body, Public.Config.StaticHtmlConfigs.GetConfig().OverwriteFiles);
    }
}